package io.renren.modules.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.student.dao.PunchDao;
import io.renren.modules.student.entity.PunchEntity;
import io.renren.modules.student.service.PunchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;


@Service("punchService")
public class PunchServiceImpl extends ServiceImpl<PunchDao, PunchEntity> implements PunchService {

    @Autowired
    PunchDao punchDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PunchEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", params.get("userId"));
        if (StringUtils.isNotBlank((String) params.get("punchDate"))) {
            wrapper.eq("punch_date", params.get("punchDate"));
        }
        IPage<PunchEntity> page = this.page(
                new Query<PunchEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PunchEntity getTodayPunch(PunchEntity entity) {
        return punchDao.getTodayPunch(entity);
    }

    @Override
    public IPage<Map<String, Object>> getStudentList(Page<Map<String, Object>> page, Map<String, Object> params) {
        return punchDao.getStudentList(page, params);
    }

    @Override
    public IPage<Map<String, Object>> getAllStudentList(Page<Map<String, Object>> page, Map<String, Object> params) {
        return punchDao.getAllStudentList(page, params);
    }

    public static void main(String[] args) throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostName();

        System.out.println("Local HostAddress:"+addr.getHostAddress());
        System.out.println("Local host name: " + hostname);
    }

}
